package br.com.tutotec.conf;

import java.lang.reflect.Field;
import java.util.List;

import br.com.tutotec.conf.annotations.FieldConf;

public class FieldsAnnotationsToConfig<T> extends ConfigFile {	
	
	
	private T instancia;
	

	public FieldsAnnotationsToConfig(Class<T> Classe) {
		
		try {
			this.instancia = Classe.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		

		LoadFields();
	}

	public void LoadFields() {
		
		Load();
		
		for (Field f : this.instancia.getClass().getFields()) {
			if(f.isAnnotationPresent(FieldConf.class)){
				FieldConf annot = (FieldConf) f.getAnnotation(FieldConf.class);
				
				String padrao = annot.ValorPadrao();
				
				try{
					
					if( f.getGenericType().hashCode()==String.class.hashCode() ){ //String
						f.set(this.instancia, getString(f.getName(), padrao));
					}else
					
					if( f.getGenericType().hashCode()==Integer.class.hashCode() || f.getGenericType().hashCode()==int.class.hashCode()){ //Integer
						padrao = (padrao.equals("")) ? "0":padrao;
						f.set(this.instancia, getInteger(f.getName(), Integer.valueOf(padrao)));
					}else
					
					if( f.getGenericType().hashCode()==Boolean.class.hashCode() || f.getGenericType().hashCode()==boolean.class.hashCode()){ //Boolean
						padrao = (padrao.equals("")) ? "false":padrao;
						f.set(this.instancia, getBoolean(f.getName(), Boolean.valueOf(padrao)));
					}
					
					if( f.getGenericType().hashCode()==Long.class.hashCode() || f.getGenericType().hashCode()==long.class.hashCode()){ //Long
						padrao = (padrao.equals("")) ? "0":padrao;
						f.set(this.instancia, getLong(f.getName(), Long.valueOf(padrao)));
					}
		
				}catch(Exception e){
					e.printStackTrace();
				}
				
			}			
		}
		
	}
	
	
	public void SaveFields() {
		
		for (Field f : this.instancia.getClass().getFields()) {
			if(f.isAnnotationPresent(FieldConf.class)){
				FieldConf annot = (FieldConf) f.getAnnotation(FieldConf.class);
		
				
				try{
					
					if( f.getGenericType().hashCode()==String.class.hashCode()  ){ //String
						this.setString(f.getName(), (String)f.get(this.instancia));
					}else
					
					if( f.getGenericType().hashCode()==Integer.class.hashCode() || f.getGenericType().hashCode()==int.class.hashCode() ){ //Integer
						this.setInteger(f.getName(), (Integer)f.get(this.instancia));
					}else
					
					if( f.getGenericType().hashCode()==Boolean.class.hashCode() || f.getGenericType().hashCode()==boolean.class.hashCode() ){ //Boolean
						this.setBoolean(f.getName(), (Boolean)f.get(this.instancia));
					}
					
					if( f.getGenericType().hashCode()==Long.class.hashCode() || f.getGenericType().hashCode()==long.class.hashCode() ){ //Long
						this.setLong(f.getName(), (Long)f.get(this.instancia));
					}
		
				}catch(Exception e){
					e.printStackTrace();
				}
				
			}			
		}
		
		Save();
	}

    public T getInstancia() {
        return instancia;
    }
	
}
